<h1 id='msg' style="color: red"></h1>

<script type="text/javascript">document.getElementById("msg").innerText="AAA";</script>

<?php
/**
 * Created by PhpStorm.
 * User: ZD-SERVER
 * Date: 2017/8/2
 * Time: 13:42
 */

//set_time_limit(60);
ignore_user_abort(1);
ob_implicit_flush(1);//直接输出到浏览器
//GBK转UTF-8
function gu($str){
    return iconv("GBK","UTF-8",$str);
}
//UTF-8转GBK
function ug($str){
    return iconv("UTF-8","GBK",$str);
}
//输出到 ID 为msg 的地方
function echoToTop($str){
    echo <<<top
<script type="text/javascript">
    document.getElementById("msg").innerText="$str";
</script>
top;
    ob_flush();//从缓存取出，送到浏览器
}

$address = '192.168.0.101';
$port=10862;

$sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if($sock===false){
    die( "socket_create() failed :reason:" . gu(socket_strerror(socket_last_error($sock))) . "<br>");
}
socket_set_nonblock($sock);
$res=socket_bind($sock,$address,$port);
echo $address.":".$port."<br>";ob_flush();
if($res===false){
    die( "socket_bind() failed :reason:" . gu(socket_strerror(socket_last_error($sock))) . "<br>");
}

socket_listen($sock,2);

echo "socket_listen<br>";ob_flush();

$msgsock=socket_accept($sock);

//if($msgsock===false){
//    die("socket_accepty() failed :reason:".gu(socket_strerror(socket_last_error($sock))) . "<br>") ;
//}

$now=time();
$stopTime=$now+20;

while ($msgsock===false){
    $now=time();
    if($now>$stopTime){
        die("20秒无连接，结束脚本");
    }
    usleep(1000);

    $msgsock=socket_accept($sock);

}


echo "socket_accept($sock)<br>";ob_flush();
//得到一个连接

$msg=ug("hello你好连接成功");
socket_write($msgsock,$msg);
echo "连接成功<br>";ob_flush();
//输出你好
$now=time();
$stopTime=$now+20;
$rdmsg=socket_read($msgsock,8192);
echo 'read client message<br>';ob_flush();
$flag=true;
$interval=1;

while ($flag){
    $now=time();
    if($now>$stopTime){
        die("20秒无消息，结束");
    }


    $rdmsg=socket_read($msgsock,8192);
    if(connection_aborted()==1){
        $msg="页面关闭，结束脚本.";
        socket_write($msgsock,ug($msg));
        socket_close($msgsock);
        socket_close($sock);
        die();
    }

    switch ($rdmsg){
        case false:
            //没收到信息,隔2秒输出一次信息
            if(time()>$stopTime-20+$interval){
                $temp=20-$interval;
                $interval+=2;
                $msg="$temp 秒无消息断开";

                echoToTop($msg);//输出到网页
                socket_write($msgsock,ug($msg));//输出到客户端
            }
            usleep(1);
            break;
        case "quit":
            echoToTop($rdmsg);
            $msg="收到quit信息，结束脚本";
            socket_write($msgsock,ug($msg));
            die("quit退出");
            break;
        case "login":
            socket_set_blocking($sock,1);
            socket_write($msgsock,"username:");
            $username=socket_read($msgsock,1024);
            socket_write($msgsock,$username."==>");
            break;
        default:
            $interval=1;
            $stopTime=time()+20;
            $strRd="received message:<h3>$rdmsg</h3>";
            $strRd=gu($strRd);
            echo $strRd;ob_flush();
            $msg=md5($rdmsg);
            socket_write($msgsock,$msg);
    }

}





socket_close($msgsock);
socket_close($sock);
echoToTop("OVER");

echo "quit,退出.<br>";ob_flush();

print("AUTO STOP");
?>

